package com.jacen.fzxs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jacen.fzxs.entity.RoleEntity;
import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.service.IRoleService;
import com.jacen.fzxs.service.IRoleUserService;
import com.jacen.fzxs.service.IUserService;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.Utils;

/**
 * 角色用户控制
 * @author Jacen
 *
 */
@Controller
public class RoleUserController extends BaseController {
	
	@Resource
	private IRoleUserService roleUserService;
	@Resource
	private IRoleService roleService;
	@Resource
	private IUserService userService;
	
	private static Logger logger = LoggerFactory.getLogger(RoleUserController.class);
	
	@RequestMapping("/roleuser/choose.do")
	public String choose(HttpServletRequest request,Integer curPage,String roleId,ModelMap modelMap) throws Exception{
		List<UserEntity> list = roleUserService.getPage(getPager(curPage, request));
		
		modelMap.addAttribute("list", list);
		modelMap.addAttribute("roleId", roleId);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"查看用户列表");
		return "user/singleChoose";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/roleuser/update.do")
	@ResponseBody
	public String postConfig(String id,String select,ModelMap modelMap){
		try{
			roleUserService.updateUserByRole(id,select);
			Map<String, Object> resultMap = new HashMap<String, Object>();
	        resultMap.put("code", 1);
	        resultMap.put("id", id);
	        return JSON.toJSONString(resultMap);
		}catch(Exception e){
			return Utils.errorJsonResp("更新角色用户异常");
		}
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/roleuser/delete.do")
	@ResponseBody
	public String delete(String roleId,String userId,ModelMap modelMap){
		try{
			roleUserService.deleteRoleUser(roleId,userId);
			Map<String, Object> resultMap = new HashMap<String, Object>();
	        resultMap.put("code", 1);
	        resultMap.put("id", roleId);
	        return JSON.toJSONString(resultMap);
		}catch(Exception e){
			return Utils.errorJsonResp("更新角色用户异常");
		}
	}
	
	@RequestMapping(value="/roleuser/list.do")
	public String list(String id,ModelMap modelMap){
		RoleEntity entity = roleService.getById(id);
		List<UserEntity> list = null;
		try{
			list = userService.getListByRoleId(id);
		}catch(Exception e){
			e.printStackTrace();
		}
		modelMap.put("list", list);
		modelMap.put("role", entity);
		return "roleuser/list";
	}
	
	/**
	 * 批量删除
	 * @param request
	 * @param ids
	 * @return
	 */
	@RequestMapping("/roleuser/batchDelete.do")
	public String batchDelete(HttpServletRequest request,String roleId,String[] ids){
		for(String id : ids){
			roleUserService.deleteRoleUser(roleId,id);
		}
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"用户："+getLocalUser(request).getUserName()+"批量删除角色用户数："+ids.length);
		return "redirect:/roleuser/list.do?id="+roleId;
	}

}
